//
//  RNOLPaySettingsViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/5/2.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLPaySettingsViewController.h"
#import "RNOLPayWayCell.h"
#import "RNOLPayWaySettingsHeaderView.h"

@interface RNOLPaySettingsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) BOOL showPayWay;

@property (nonatomic,strong) RNOLPayWaySettingsHeaderView *header;


@end



@implementation RNOLPaySettingsViewController

- (NSMutableArray *)dataSource{
    if (_dataSource==nil) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , KHIGHT) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        adjustsScrollViewInsets_NO(_tableView, self);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine ;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setEditing:YES animated:YES];//开启编辑权限
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([RNOLPayWayCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([RNOLPayWayCell class])];
        UIView *headerBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , 125)];
        headerBg.backgroundColor = KWhiteColor;
        RNOLPayWaySettingsHeaderView *header = [RNOLPayWaySettingsHeaderView FromXIB];
        header.frame = headerBg.bounds;
        [headerBg addSubview: header];
        self.header = header;
        _tableView.tableHeaderView = headerBg;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview: _tableView];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBgColor;
    [self setupNavBarTitleViewWithText:@"支付设置"];
    [self setup];
    [self bindingSwitch];
}

- (void)setup{
    self.showPayWay = YES;//默认
    self.dataSource = [@[@"账户余额",@"招商银行卡1198",@"招商银行卡2533"] mutableCopy];
    [self.tableView reloadData];
}


#pragma mark- 绑定开关
- (void)bindingSwitch{
    @weakify(self);
    [self.header.payWaySwitch.rac_newOnChannel subscribeNext:^(NSNumber * _Nullable x) {
       @strongify(self);
        BOOL enable = [x boolValue];
        self.showPayWay = enable;
        [self.tableView reloadData];
    }];
}


#pragma mark -  UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.showPayWay? self.dataSource.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RNOLPayWayCell *cell = [tableView dequeueReusableCellWithIdentifier: NSStringFromClass([RNOLPayWayCell class])];
    cell.nameLabel.text = self.dataSource[indexPath.row];
    cell.separatorInset = UIEdgeInsetsMake(0, 15, 0, 0);
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *bgView  = [RNOLUnitTool createViewWithBackgroundColor:KWhiteColor];
    bgView.frame = CGRectMake(0, 0, KWIDTH , 44);
    
    UILabel *leftLabel = [RNOLUnitTool createLabelWithText:@"系统将根据下面排列顺序扣款" textColor:kTextLightColor fontSize:12];
    leftLabel.frame = CGRectMake(15, 0, 200 , 30);
    leftLabel.centerY = bgView.centerY;
    leftLabel.text = self.header.payWaySwitch.on? @"系统将根据下面排列顺序扣款" : @"如不开启,系统将自动选择支付方式";
    [bgView addSubview: leftLabel];
    
    UILabel *rightLabel = [RNOLUnitTool createLabelWithText:@"拖动" textColor:kTextLightColor fontSize:12 textAlignment:NSTextAlignmentRight];
    rightLabel.frame = CGRectMake(KWIDTH - 65, 0, 50 , 30);
    rightLabel.centerY = leftLabel.centerY;
    rightLabel.text = self.header.payWaySwitch.on? @"拖动" : @"";
    [bgView addSubview: rightLabel];

    return bgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44.0f;
}


//对cell的编辑样式进行设置, 点击cell 右边的按钮可以 拖动进行重新排序
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}

//允许拖动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//对数据源进行重新排序
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSMutableArray *dataArray = self.dataSource;
    [dataArray exchangeObjectAtIndex: sourceIndexPath.row  withObjectAtIndex: destinationIndexPath.row];
    [tableView reloadData];
    self.dataSource = dataArray;
}






@end
